'use strict';

const Service = require('egg').Service;

class MysqlService extends Service {
    async insert(tableName, json) { //添加一条数据
        try {
            const result = await this.app.mysql.insert(tableName, json);
            /* console.log('---------------')
            console.log(result)
            console.log('---------------') */
            /* {
                fieldCount: 0,
                affectedRows: 1,
                insertId: 35,
                serverStatus: 2,
                warningCount: 0,
                message: '',
                protocol41: true,
                changedRows: 0 } */
            if (result.affectedRows === 1) {
                return true
            } else {
                console.log(result.message)
                return false
            }
        } catch (err) {
            console.log(err)
        }
    }

    async find(tableName, json) { //查询一条记录
        const result = await this.app.mysql.get(tableName, json);
        return result;
    }

    async findAll(tableName,options) { //查询全表
        const result = await this.app.mysql.select(tableName,options);
        return result;
    }

    async update(tableName, json, options) { // 更新表中的记录
        const result = await this.app.mysql.update(tableName, json, options);
        if (result.affectedRows === 1) {
            return true
        } else {
            console.log(result.message)
            return false
        }
    }

    async delete(tableName, json) { //删除一条数据
        const result = await this.app.mysql.delete(tableName, json);
        if (result.affectedRows === 1) {
            return true;
        } else {
            console.log(result.message);
            return false;
        }
    }
}

module.exports = MysqlService;